import { DataNode, TransformResult } from '@/types/app';

/**
 * 递归遍历数据结构，生成路由路径和按钮权限数组
 * @param data - 原始数据
 * @param parentPath - 当前路径的前缀
 * @returns 包含 routePaths 和 buttonPermissions 的对象
 */
export function transformData(
  data: DataNode,
  parentPath: string = '',
): TransformResult {
  const menus: string[] = [];
  const perms: string[] = [];

  for (const key in data) {
    if (data.hasOwnProperty(key)) {
      // 构建当前路径
      const currentPath = parentPath ? `${parentPath}:${key}` : key;
      menus.push(currentPath);

      const value = data[key];

      if (Array.isArray(value)) {
        // 如果值是数组，则生成按钮权限
        value.forEach((action) => {
          perms.push(`${currentPath}:${action}`);
        });
      } else if (typeof value === 'object' && value !== null) {
        // 如果值是对象，则递归处理
        const { menus: subRoutePaths, perms: subButtonPermissions } =
          transformData(value, currentPath);
        menus.push(...subRoutePaths);
        perms.push(...subButtonPermissions);
      }
    }
  }

  return { menus, perms };
}
